package com.yyge.controller.admin.__.__;

import com.yyge.common.controller.BaseController;
import com.yyge.common.enums.AckCode;
import com.yyge.common.vo.req.PageReq;
import com.yyge.common.vo.resp.PageResp;
import com.yyge.controller.admin.__.__.req._____Req;
import com.yyge.controller.admin.__.__.req._____AddReq;
import com.yyge.controller.admin.__.__.req._____EditReq;
import com.yyge.controller.admin.__.__.resp._____Resp;
import com.yyge.controller.admin.mapstruct._____Converter;
import com.yyge.entity._____;
import com.yyge.service._____Service;
import com.yyge.common.vo.resp.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.crypto.spec.PSource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/__.__")
public class _____Controller extends BaseController<_____,Object> {
    
    public _____Controller() {
        this.converter=_____Converter.class;
    }
    
    // 注入业务逻辑层
    @Autowired/*自动装配*/
    private _____Service service;

    @GetMapping<---PREFIX--->/* GET(SELECT): 查询 */
    public R<PageResp<_____Resp>> selectAll(_____Req req) throws Exception {
         // 通过BaseController将对象进行转换过滤无效条件并返回Wrapper;
        QueryWrapper<_____> queryWrapper = this.getWrapper(req);
        PageResp<_____Resp> pageResp =this.page(req,pageInfo ->service.page(pageInfo,queryWrapper));
        return R.okHasData(pageResp);
    }


    @PostMapping/* POST(CREATE): 新增 */
    public R add(@RequestBody _____AddReq req) throws Exception {
        _____ entity = getEntity(req);
        long count = service.count(new QueryWrapper<>(entity));
        if(count>0){
            return R.error(AckCode.SYSTEM_SERVICE_ADD_REPEAT_ERROR);
        }
        boolean save = service.save(entity);

        return save?R.ok("新增成功！"):R.error(AckCode.SYSTEM_SERVICE_ADD_ERROR);
    }

    @PutMapping/* POST(CREATE): 修改 */
    public R updata(@RequestBody _____EditReq req) throws Exception {
        _____ entity = getEntity(req);
        QueryWrapper<_____> wrapper= new QueryWrapper<>(entity);
        long count = service.count(wrapper);
        if(count>0){
            return R.error(AckCode.SYSTEM_SERVICE_ADD_REPEAT_ERROR);
        }

        boolean save = service.update(wrapper);
        return save?R.ok("修改成功！"):R.error(AckCode.SYSTEM_SERVICE_MODIFY_ERROR);
    }

    @PatchMapping/* PUT(UPDATE): 部分更新 */
    public R patch(@RequestBody _____Req req){
        return null;
    }

    @DeleteMapping("/{id}")/* DELETE(DELETE): 删除&&批量删除 */
    public R delete(@PathVariable("id") Integer[] list){
        boolean i = service.removeByIds(Arrays.asList(list));
        return i?R.ok("修改成功！"):R.error(AckCode.SYSTEM_SERVICE_DELETE_ERROR);
    }
}

